package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.dto.ProductDTO;
import net.wanho.po.Product;
import net.wanho.service.ProductService;
import net.wanho.mapper.ProductMapper;
import net.wanho.vo.PageInfo;
import net.wanho.vo.ProductVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 王洋
* @description 针对表【product(商品表)】的数据库操作Service实现
* @createDate 2024-09-03 17:43:36
*/
@Service
@RequiredArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product>
    implements ProductService{
    private final ProductMapper productMapper;

    @Override
    public PageInfo<ProductVO> productPageList(Integer pageNum, Integer pageSize, ProductDTO productDTO) {
        Page<ProductVO> page = new Page<>(pageNum,pageSize);
        QueryWrapper<ProductVO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!ObjectUtils.isEmpty(productDTO.getStoreId()),"s.store_id",productDTO.getStoreId());
        queryWrapper.like(!ObjectUtils.isEmpty(productDTO.getProductName()),"product_name",productDTO.getProductName());
        queryWrapper.eq(!ObjectUtils.isEmpty(productDTO.getBrandName()),"b.brand_name",productDTO.getBrandName());
        queryWrapper.eq(!ObjectUtils.isEmpty(productDTO.getTypeName()),"t.type_name",productDTO.getTypeName());
        queryWrapper.eq(!ObjectUtils.isEmpty(productDTO.getSupplyName()),"s2.supply_name",productDTO.getSupplyName());
        queryWrapper.eq(!ObjectUtils.isEmpty(productDTO.getPlaceName()),"p2.place_name",productDTO.getPlaceName());
        queryWrapper.eq(!ObjectUtils.isEmpty(productDTO.getUpDownState()),"up_down_state",productDTO.getUpDownState());
        if(productDTO.getIsOverDate()!=null&&productDTO.getIsOverDate()==1){
            queryWrapper.ge("supp_date", LocalDateTime.now());
        } else if (productDTO.getIsOverDate()!=null&&productDTO.getIsOverDate()==0) {
            queryWrapper.lt("supp_date",LocalDateTime.now());
        }

        Page<ProductVO> productPageList= this.baseMapper.productPageList(page, queryWrapper);

        PageInfo<ProductVO> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setTotalNum(page.getTotal());
        pageInfo.setPageCount(page.getPages());
        pageInfo.setLimitIndex((page.getCurrent()-1) * page.getSize());
        pageInfo.setResultList(page.getRecords());

        return pageInfo;




    }

    @Override
    public Boolean productNumCheck(String productNum) {

       Product product = productMapper.selectOne(new QueryWrapper<Product>().eq("product_num",productNum));
       if (ObjectUtils.isNotEmpty(product)){
           return false;
       }else {
           return true;
       }
       }


}




